<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	public function index(){
	
		$this->display();
	}
	public function login(){
		
		if(IS_POST){
			$username=I('username');
			$password=md5(I('password'));
			$user=M('user')->where(array('username'=>$username))->field('username,password,power')->find();
			if($password!=$user['password']){
				$this->error("登录失败");
			}else {
				session('username',$username);
				if($user['power'])session('power',$user['power']);
				
				$this->success("您已登录成功",U('Index/index'));
			}
		}

	}
	public function signup(){
		if(IS_POST){
			
			if(I('username')==""||I('password')==""){
				$this->error("注册失败");
			}
			$data['username']=I('username');
			$data['password']=md5(I('password'));
			if(M('user')->add($data)){
				session('username',I('username'));
				$this->success("注册成功！即将跳转到首页",U('Index/index'));
			}else $this->error('注册失败！');

		}else{
			$this->display();
		}
	}
	public function checkunique(){
		$username=$_POST["param"];
		$result=M('user')->where(array('username'=>$username))->find();

		if($result==null||!$result)
			{
				echo "y";//验证通过
			}else echo "用户名已存在";
	}
	public function logout(){
	    session(null);
	    $this->success("退出成功");
	}
}
